'use strict'

const Joi = require('@hapi/joi')
const t = (module.exports = require('../tester').createServiceTester())
const {
  twoVulnerabilitiesSvg,
  zeroVulnerabilitiesSvg,
} = require('./snyk-test-helpers')

t.create('valid repo')
  .get('/snyk/snyk.json')
  .timeout(20000)
  .expectBadge({
    label: 'vulnerabilities',
    message: Joi.number().required(),
  })

t.create('non existent repo')
  .get('/badges/not-real.json')
  .timeout(20000)
  .expectBadge({
    label: 'vulnerabilities',
    message: 'repo or manifest not found',
  })

t.create('valid target manifest path')
  .get('/snyk/vulndb-fixtures/packages/cli/0.1.0/package.json.json')
  .timeout(20000)
  .expectBadge({
    label: 'vulnerabilities',
    message: Joi.number().required(),
  })

t.create('invalid target manifest path')
  .get('/badges/shields/gh-badges/requirements.txt.json')
  .timeout(20000)
  .expectBadge({
    label: 'vulnerabilities',
    message: 'repo or manifest not found',
  })

t.create('repo has no vulnerabilities')
  .get('/badges/shields.json')
  .intercept(nock =>
    nock('https://snyk.io/test/github/badges/shields')
      .get('/badge.svg')
      .reply(200, zeroVulnerabilitiesSvg)
  )
  .expectBadge({
    label: 'vulnerabilities',
    message: '0',
    color: 'brightgreen',
  })

t.create('repo has vulnerabilities')
  .get('/badges/shields.json')
  .intercept(nock =>
    nock('https://snyk.io/test/github/badges/shields')
      .get('/badge.svg')
      .reply(200, twoVulnerabilitiesSvg)
  )
  .expectBadge({
    label: 'vulnerabilities',
    message: '2',
    color: 'red',
  })

t.create('target manifest file has no vulnerabilities')
  .get('/badges/shields/gh-badges/package.json.json')
  .intercept(nock =>
    nock('https://snyk.io/test/github/badges/shields')
      .get('/badge.svg')
      .query({
        targetFile: 'gh-badges/package.json',
      })
      .reply(200, zeroVulnerabilitiesSvg)
  )
  .expectBadge({
    label: 'vulnerabilities',
    message: '0',
    color: 'brightgreen',
  })

t.create('target manifest file has vulnerabilities')
  .get('/badges/shields/gh-badges/package.json.json')
  .intercept(nock =>
    nock('https://snyk.io/test/github/badges/shields')
      .get('/badge.svg')
      .query({
        targetFile: 'gh-badges/package.json',
      })
      .reply(200, twoVulnerabilitiesSvg)
  )
  .expectBadge({
    label: 'vulnerabilities',
    message: '2',
    color: 'red',
  })
